package ph.gov.marikina.mcsmtms.dao.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Logger;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import ph.gov.marikina.mcsmtms.dao.MobileUserReportDAO;
import ph.gov.marikina.mcsmtms.domain.MobileUserReport;
import ph.gov.marikina.mcsmtms.domain.User;
import ph.gov.marikina.mcsmtms.utils.Utils;

@Transactional
public class MobileUserReportDAOImpl implements MobileUserReportDAO {
	/** The log. */
	private static final Logger log = Logger.getLogger(MobileUserReportDAOImpl.class.getName());
	
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void addMobileUserReport(MobileUserReport mobileUserReport) {
		Session session = sessionFactory.openSession();
		log.info("mobileUserReport before save: " + mobileUserReport.getDetails());
		
		User user = new User();
		user.setId(mobileUserReport.getReporter());
		mobileUserReport.setUser(user);
		
		session.save(mobileUserReport);
		session.close();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<MobileUserReport> getAllMobileUserReports() {
		Query query = this.sessionFactory.getCurrentSession()
							.createQuery("from MobileUserReport");
//                .createQuery("from MobileUserReport " +
//                				"where reportDateTime >= ? and reportDateTime <= ?");
		
//		Calendar cal = new GregorianCalendar();
//		Date fromDate = new Date();
//		cal.setTime(fromDate);
//		
//		cal.set(cal.HOUR_OF_DAY, 0);
//		cal.set(cal.MINUTE, 0);
//		cal.set(cal.SECOND, 0);
//		fromDate = cal.getTime();
//		
//		cal.set(cal.HOUR_OF_DAY, 23);
//		cal.set(cal.MINUTE, 59);
//		cal.set(cal.SECOND, 59);
//		Date endDate = cal.getTime();
		
		return (List) query
				//.setParameter(0, fromDate)
				//.setParameter(1, endDate)
				.list();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public MobileUserReport getMobileUserReport(String id) {
		Query query = this.sessionFactory.getCurrentSession()
                .createQuery("from MobileUserReport where id = :id");
		query.setParameter("id", Integer.parseInt(id));
		
		return (MobileUserReport) query.list().get(0);
	}
	
	public void updateSituation(MobileUserReport mobileUserReport) {
		Session session = sessionFactory.openSession();
		
		session.getTransaction().begin();
		session.update(mobileUserReport);
		session.getTransaction().commit();

		session.close();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MobileUserReport> getMobileUserReportsForDate(Date date) {
//		Calendar gc = new GregorianCalendar();
//		gc.setTimeZone(TimeZone.getTimeZone("Asia/Manila"));
//		gc.setTime(date);
		
		Date fromDate = Utils.getFirstTimeDate(date);
		Date toDate = Utils.getLastTimeDate(date);
		
		
		Query query = this.sessionFactory.getCurrentSession().createQuery("from MobileUserReport " +
	    				"where reportDateTime >= ? and reportDateTime <= ?");

		return (List<MobileUserReport>) query
			.setParameter(0, fromDate)
			.setParameter(1, toDate)
			.list();
	}
		
}
